﻿/* Copyright (c) 2011 Mahir Iqbal
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.bayvakoof.dewall.geom
{
	import flash.display.Shape;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Mahir Iqbal
	 */
	public class Axis
	{
		// Axis orientation constants.
		public static const HORIZONTAL:int = 1;
		public static const VERTICAL:int = 0;
		
		public var location:Number;
		public var direction:int;
		
		public function Axis(location:Number, direction:int)
		{
			this.location = location;
			
			if (direction != 0 && direction != 1)
				throw Error("Invalid direction for axis.");
				
			this.direction = direction;
		}
		
		/**
		 * Returns the closest point on this Line to Point point (computed by the perpendicular).
		 * @param	point
		 * @return
		 */
		public function getClosestPointOnAxis(point:Point):Point
		{
			if (direction == HORIZONTAL)
				return new Point(point.x, location);
			else
				return new Point(location, point.y);
		}
		
		/**
		 * 
		 * @param	point
		 * @return
		 */
		public function getSide(p:Point):int
		{
			var side:Number;
			
			if (direction == HORIZONTAL)
				side = location - p.y;
			else
				side = location - p.x;
				
			if (side == 0)
				return 0;
			else
				return (side > 0) ? 1 : -1;
		}
		
	}
	
}